const message = require("../models/message")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
            message.find({
                UUID: fields.UUID
            },{
                __v:0
            }, {
                    sort: [[['_id', -1]]]
                }, (err, docs) => {
                    if (err) {
                        res.json({ 'state': false, 'msg': err });
                    } else {
                        // let results = {
                        //     unread: [],
                        //     readed: [],
                        //     trash: []
                        // }

                        // for (var index in docs) {
                        //     switch (docs[index].status) {
                        //         case "unread":
                        //             results.unread.push(docs[index])
                        //             break
                        //         case "readed":
                        //             results.readed.push(docs[index])
                        //             break
                        //         case "trash":
                        //             results.trash.push(docs[index])
                        //         default:
                        //             break
                        //     }
                        // }
                        let results = {
                            application: {
                                list: [],
                                unread: 0
                            },
                            answer_notification: {
                                list: [],
                                unread: 0
                            },
                            user_feedback: {
                                list: [],
                                unread: 0
                            },
                            system: {
                                list: [],
                                unread: 0
                            },
                            trash: []
                        }
                        for (var index in docs) {
                            if(docs[index].status==="trash"){
                                results.trash.push(docs[index])
                                continue
                            }
                            switch (docs[index].type) {
                                case "application":
                                    results.application.list.push(docs[index])
                                    if(docs[index].status==="unread"){
                                        results.application.unread++
                                    }
                                    break
                                case "answer_notification":
                                    results.answer_notification.list.push(docs[index])
                                    if(docs[index].status==="unread"){
                                        results.answer_notification.unread++
                                    }
                                    break
                                case "user_feedback":
                                    results.user_feedback.list.push(docs[index])
                                    if(docs[index].status==="unread"){
                                        results.user_feedback.unread++
                                    }
                                case "system":
                                    results.system.list.push(docs[index])
                                    if(docs[index].status==="unread"){
                                        results.system.unread++
                                    }
                                default:
                                    break
                            }
                        }
                        console.log(results.trash)
                        res.json({ 'state': true, 'results': results });
                    }
                })
        }

    });

});

module.exports = router;